package com.pixiv.controller;

import com.pixiv.Entity.User;
import com.pixiv.service.LoginService;
import com.pixiv.tools.FilePragram;
import com.pixiv.tools.SendPrintWriterPacket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Controller
@RequestMapping("/Login")
public class LoginController {

    @Autowired
    private LoginService loginService;


    @ResponseBody
    @RequestMapping("sendLoginPacket")
    public void SendLoginPacket(HttpServletRequest req, HttpServletResponse resp)
    {

        String userName = req.getParameter("User");
        String PWD = req.getParameter("PWD");

        User u = loginService.getUserByLoginParameter(userName,PWD);
        if (u != null)
        {
            HttpSession hs = req.getSession();
            hs.setAttribute("GLOBAL_USER_OBJECT",u);

            Date d = loginService.getLastLoginTimeByUserID(u.getUserID());

            if (d == null)
            {
                applyUserFristLoginEvent(req);
                //应用首次进入时触发的函数
            }
            loginService.initLoginUserData(u);//初始化用户
            loginService.flushUserLoginTime(u.getUserID());//刷新用户登录时间
            //TempStoragePictureDAO.initUserTempPicture(u.getUserID() );//刷新用户存储临时表
            hs.setAttribute("PixivUpPictureStorageServlet_TempPictureID",1);
            SendPrintWriterPacket.sendFlag(resp,1);

        }else{
            SendPrintWriterPacket.sendFlag(resp,0);
        }
        //System.out.println(u);
    }

    public void applyUserFristLoginEvent(HttpServletRequest req) {
        //System.out.printf("首次进入");
        HttpSession hs = req.getSession();
        User u = (User) hs.getAttribute("GLOBAL_USER_OBJECT");

        //新生成对应的用户路径
        ApplicationContext ac = new ClassPathXmlApplicationContext("Spring.xml");
        FilePragram fp = ac.getBean(FilePragram.class);
        fp.setPath("user/user_" + u.getUserID());

        if (!fp.exist())
        {
            fp.getFile().mkdirs();
            fp.mkdir("icon");
            fp.mkdir("tempStorage");
            fp.mkdir("auditPicture");
            fp.mkdir("etc");

            //插入用户头像的id
            loginService.insertInitUserIconByPath(u.getUserID() );
            loginService.updateUserType(0,u.getUserID() );
        }
        loginService.insertInitUserLevelInfor(u.getUserID() );
        fp.close();


    }

    /**
     * 前往登录的行为
     * @return
     */
    @RequestMapping("gotoLogin")
    public ModelAndView gotoLogin(){
        return new ModelAndView("/pixiv/login");
    }




}
